package com.laiketui.app.services.dubbo;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.laiketui.app.api.JssdkService;
import com.laiketui.app.common.consts.AppConst;
import com.laiketui.common.mapper.PaymentConfigModelMapper;
import com.laiketui.common.utils.weixin.Jssdk;
import com.laiketui.domain.lktconst.ErrorCode;
import com.laiketui.root.annotation.DubboApiMethod;
import com.laiketui.core.exception.LaiKeAPIException;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;


/**
 * h5授权信息接口
 *
 * @author Trick
 * @date 2020/9/23 9:22
 */
@DubboService(version = "1.0.0")
public class JssdkDubboServiceImpl implements JssdkService {

    private final Logger logger = LoggerFactory.getLogger(JssdkDubboServiceImpl.class);

    @Autowired
    PaymentConfigModelMapper paymentConfigModelMapper;

    @Autowired
    Jssdk jssdk;

    @DubboApiMethod(apiKey = "com.laike.app.getData",
            login = false, module = AppConst.MODULE_NAME, version = "1.0.0")
    @Override
    public Map<String, String> getData(@NotNull int storeId, @NotNull String url) throws LaiKeAPIException {
        Map<String, String> resultMap = new HashMap<>(16);
        try {

            String configData = paymentConfigModelMapper.getPaymentConfigData(storeId);
            if (!StringUtils.isEmpty(configData)) {
                Map<String,Object> paymentConfigMap = JSON.parseObject(configData,new TypeReference<Map<String,Object>>(){});
                String appid = paymentConfigMap.get("appid") + "";
                String secret = paymentConfigMap.get("secret") + "";

                //获取ticket todo 暂时注释
//                resultMap = jssdk.getTicket(url, appid, secret);
            }

        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "getUrl");
        }

        return resultMap;
    }

}

